import { Component } from '@angular/core';
import { Contact } from '@capacitor-community/contacts'
import { Plugins } from '@capacitor/core'
import { CallNumber } from '@ionic-native/call-number/ngx';
import { SMS } from '@ionic-native/sms/ngx';
const { Contacts } = Plugins
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { BatteryStatus } from '@ionic-native/battery-status/ngx';
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  subscription: any
  constructor(
    public callNumber: CallNumber,
    public sms: SMS,
    public camera: Camera,
    public batteryStatus: BatteryStatus
  ) {

  }
  findContact() {
    Contacts.getContacts().then(res => {
      console.log(res.contacts)
    })
  }
  call() {
    this.callNumber.callNumber('18214943376', true).then(res => {
      console.log(res)
    }).catch(e => {
      console.log(e)
    })
  }
  send() {
    this.sms.send('18214943376', 'test').then(res => {
      console.log(res)
    })
  }
  photos() {

    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64 (DATA_URL):
      let base64Image = 'data:image/jpeg;base64,' + imageData;
      console.log(base64Image)
    }, (err) => {
      // Handle error
    });
  }

  powerStart() {
    // watch change in battery status
    this.subscription = this.batteryStatus.onChange().subscribe(status => {
      console.log(status.level, status.isPlugged);
    });
  }
  end() {
    this.subscription.unsubscribe();
  }

}
